package com.example.smartmallserver.demos.web.controller;

import com.example.smartmallserver.demos.web.pojo.Cart;
import com.example.smartmallserver.demos.web.pojo.Goods;
import com.example.smartmallserver.demos.web.pojo.OrderItem;
import com.example.smartmallserver.demos.web.pojo.User;
import com.example.smartmallserver.demos.web.service.IndexService;
import com.example.smartmallserver.demos.web.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    IndexService indexService;
    @Autowired
    HttpSession session;

    @GetMapping("/checkout/order")
    Result checkOrder(String mode, Integer cartIds[],Integer goodsId, Integer goodsNum){
        List<Cart> res = new ArrayList<>();
        User user = indexService.getUserById(session.getAttribute("id"));
        double totalPrice=0;
        Integer totalCount=0;
        if (mode.equals("cart")){
            for (int i=0;i< cartIds.length;i++){
                Cart c =indexService.checkOrder(cartIds[i]);
                res.add(c);
                totalPrice+=c.getGoodsNum()*c.getGoodsPrice();
                totalCount+=c.getGoodsNum();
            }
        }
        if (mode.equals("buyNow")){

            Goods goods = indexService.getGoodsById(goodsId);
            Cart cart = new Cart();
            cart.setGoodsName(goods.getName());
            cart.setGoodsPrice(goods.getNew_price());
            cart.setGoodsNum(goodsNum);
            cart.setGoodsImage(goods.getImage());
            res.add(cart);
            totalPrice = cart.getGoodsPrice()*cart.getGoodsNum();
            totalCount = cart.getGoodsNum();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("order",res);
        map.put("personal",user);
        map.put("totalPrice",totalPrice);
        map.put("totalCount",totalCount);
        return Result.success(map);
    }

    @PostMapping("/checkout/submit")
    Result checkSubmit(@RequestBody HashMap<String,Object> map){
        if (map.get("mode").equals("cart")){
            System.out.println("cart");
//            String[] stringArray = map.get("cartIds").toString().split(",");
//            Integer[] integerArray = new Integer[stringArray.length];
//            for (int i = 0; i < stringArray.length; i++) {
//                integerArray[i] = Integer.parseInt(stringArray[i]);
//            }
            indexService.submitCart((String) map.get("remark"), (String) map.get("cartIds") );
        }
        if (map.get("mode").equals("buyNow")){
            System.out.println("buyNow");
            indexService.submitBuyNow((String) map.get("remark"),(String) map.get("goodsId"),(String) map.get("goodsNum"));
        }
        return Result.success();
    }

    @GetMapping("/order/list")
    Result getOrderListByStatus(String status){
        List<Map<String,Object>> OrderList = indexService.getOrderListByStatus(status,(Integer) session.getAttribute("id"));
//        Map<String,Object> map = new HashMap<>();
//        map.put("total",0);
//        map.put("list",OrderList);
        return Result.success(OrderList);
    }
}
